<script setup>
import { ref } from 'vue'
import axios from 'axios'
let dialogVisible = ref(false)
const formData = ref({
    id:1,
    name:'',
    address:''
})
const handleOpen = (row) => {
    formData.value.id= row.id
    formData.value.name= row.name
    formData.value.address= row.address
    dialogVisible.value = !dialogVisible.value
}
const emit = defineEmits(['updateData'])
const handleCommit = async ()=>{
    //1.发送请求
    await axios.put('edit',{data:{
        id:formData.value.id,
        name:formData.value.name,
        address: formData.value.address
    }})
    //2.关闭对话框
    dialogVisible.value = false
    //3.通知父组件获取新数据
    emit('updateData')
}
defineExpose({
    handleOpen
})
</script>
<template>
    <el-dialog v-model="dialogVisible" title="编辑" width="35%" >
        <el-form label-width="100px" :model="formData" style="max-width: 460px">
            <el-form-item label="Name">
                <el-input v-model="formData.name" placeholder="请输入姓名"/>
            </el-form-item>
            <el-form-item label="Address">
                <el-input v-model="formData.address" placeholder="请输入籍贯"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="dialogVisible = false">Cancel</el-button>
                <el-button type="primary" @click="handleCommit">
                    Confirm
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>
<style scoped></style>